package com.dubbo.controller;

import com.dubbo.common.check.CheckUtil;
import com.dubbo.common.redis.RedisDao;
import com.dubbo.common.result.ResultInfo;
import com.dubbo.common.security.SecurityUtils;
import com.dubbo.domain.User;
import com.dubbo.service.business.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 订单controller
 * @author chenluqiang
 * @date 2018年12月18日 下午4:26:22 
 *
 */
@RestController
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private RedisDao redisDao;
	
	/**
	 * 订单列表
	 * @author chenluqiang
	 * @date 2018年12月17日 下午2:42:37 
	 * @param type 类型 0=新订单 1=未接单 2=审核中 3=待发货 4=待收货 5=已完成
	 * @return
	 */
	@PostMapping("/list.wx")
	public Map<String,Object> list(HttpServletRequest request,Long pageStart,Long pageTotal ,Integer type) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:query", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderList(user,pageStart,pageTotal,type);
	}
	
	/**
	 * 订单详情
	 * @author chenluqiang
	 * @date 2018年12月18日 上午11:16:15 
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/detail.wx")
	public Map<String,Object> detail(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:see", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderDetail(user,orderId);
	}
	
	/**
	 * (分销商或代理商)拒绝接单
	 * @author wujunyuan
	 * @date 2018年12月25日  上午10:04:42
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/refuse.wx")
	public Map<String,Object> refuse(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderRefuse(user,orderId);
	}
	
	/**
	 * 提交审核
	 * @author wujunyuan
	 * @date 2018年12月25日  下午4:40:32
	 * @param request
	 * @param orderId 订单id
	 * @param addressee 上级收件人名称
	 * @param addresseephone 上级收件人电话
	 * @param address 上级收货地址
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/submit.wx")
	public Map<String,Object> refuseOrSubmit(HttpServletRequest request,String orderId,
			String addressee,String addresseephone,String address) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderSubmit(user,orderId,addressee,addresseephone,address);
	}
	
	/**
	 * 订单删除
	 * @author chenluqiang
	 * @date 2018年12月25日 上午10:11:06 
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/del.wx")
	public Map<String,Object> del(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
//		SecurityUtils.checkPermission("sys:order:del", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.delOrder(user,orderId);
	}
	
	/**
	 * 申请取消订单
	 * @author wujunyuan
	 * @date 2018年12月25日  下午2:44:28
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/cancel.wx")
	public Map<String,Object> cancel(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderCancel(user,orderId);
	}
	
	/**
	 * (分销商/代理商)确认取消订单
	 * @author wujunyuan
	 * @date 2018年12月25日  下午3:17:36
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/confirmCancel.wx")
	public Map<String,Object> confirmCancel(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderConfirmCancel(user,orderId);
	}
	
	/**
	 * 确认收货
	 * @author wujunyuan
	 * @date 2018年12月25日  下午3:47:19
	 * @param request
	 * @param orderId 订单id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/confirmReceived.wx")
	public Map<String,Object> confirmReceived(HttpServletRequest request,String orderId) throws Exception{
		//获取用户信息
		User user = (User) CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.orderConfirmReceived(user,orderId);
	}
	
	/**
	 * 修改订单收货地址
	 *
	 * @author lihongwu
	 * 2019年1月18日 下午5:31:14
	 * @param request
	 * @param orderId
	 * @param addressId
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/editReciverAddress.wx")
	public Map<String,Object> editReciverAddress(HttpServletRequest request,String orderId,String addressId) throws Exception{
		//获取用户信息
		CheckUtil.checkOline3(request);
		//校验权限
		SecurityUtils.checkPermission("sys:order:edit", redisDao, request.getHeader(ResultInfo.USERTOKEN));
		return orderService.editReciverAddress(orderId,addressId);
	}
	
}
